#include"CS_Logic.h"
#include"CS_Global.h"

unsigned char currentSelectorPosition = 0;
unsigned char currentDieSide = 0;
unsigned char currentPlayer = 1;
unsigned char numberOfPlayers = 4;
unsigned char playerTrays[4][4];
unsigned char playerCounts[4];

void initializeGame(void)
{
	int counter = 0, j;

	currentSelectorPosition = 0;
	currentPlayer = 0;

	gameBoard[0] = 'A';
	gameBoard[1] = 'A';
	gameBoard[2] = 'A';
	gameBoard[3] = 'A';

	gameBoard[4] = 'K';
	gameBoard[5] = 'K';
	gameBoard[6] = 'K';
	gameBoard[7] = 'K';

	gameBoard[8] = 'Q';
	gameBoard[9] = 'Q';
	gameBoard[10] = 'Q';
	gameBoard[11] = 'Q';

	gameBoard[12] = 'J';
	gameBoard[13] = 'J';
	gameBoard[14] = 'J';
	gameBoard[15] = 'J';

	for(counter = 0; counter < 4; ++counter)
	{
		for(j = 0; j<4; ++j)
		{
			playerTrays[counter][j] = 0;
		}
		playerCounts[counter] = 0;
	}
}

bool processRoll(void)
{
	unsigned char counter;
	unsigned char value;
	unsigned char index;
	unsigned char trayValue;
	if(DIE_SIDES[currentDieSide] == 'S') return false;

	index = currentSelectorPosition + (4 * currentDieSide);
	value = gameBoard[index];

	if(value != CH_SPACE)
	{
		for(counter = 0; counter < 4; ++counter)
		{
			trayValue = playerTrays[currentPlayer][counter] & 0x0F;
			switch(value)
			{
			case 'A': if(trayValue == 0x01) return false; break;
			case 'K': if(trayValue == 0x02) return false; break;
			case 'Q': if(trayValue == 0x04) return false; break;
			case 'J': if(trayValue == 0x08) return false; break;
			}
		}

		gameBoard[index] = CH_SPACE;
	
		switch(currentSelectorPosition)
		{
		case 0:	// Black, Red, Green, Blue
			switch(value)
			{
			case 'A': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x01 + (COLOR_BLACK << 4); break;
			case 'K': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x02 + (COLOR_BLUE << 4); break;
			case 'Q': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x04 + (COLOR_GREEN << 4); break;
			case 'J': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x08 + (COLOR_RED << 4); break;
			}			
			break;

		case 1: // Blue, Black, Red, Green
			switch(value)
			{
			case 'A': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x01 + (COLOR_RED << 4); break;
			case 'K': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x02 + (COLOR_BLACK << 4); break;
			case 'Q': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x04 + (COLOR_BLUE << 4); break;
			case 'J': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x08 + (COLOR_GREEN << 4); break;
			}			
			break;

		case 2: // Green, Blue, Black, Red
			switch(value)
			{
			case 'A': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x01 + (COLOR_GREEN << 4); break;
			case 'K': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x02 + (COLOR_RED << 4); break;
			case 'Q': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x04 + (COLOR_BLACK << 4); break;
			case 'J': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x08 + (COLOR_BLUE << 4); break;
			}			
			break;

		case 3: // Red, Green, Blue, Black
			switch(value)
			{
			case 'A': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x01 + (COLOR_BLUE << 4); break;
			case 'K': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x02 + (COLOR_GREEN << 4); break;
			case 'Q': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x04 + (COLOR_RED << 4); break;
			case 'J': playerTrays[currentPlayer][playerCounts[currentPlayer]] = 0x08 + (COLOR_BLACK << 4); break;
			}			
			break;

		}
		//gotoxy(0, 22); cprintf("%X", playerTrays[currentPlayer][playerCounts[currentPlayer]]);
		playerCounts[currentPlayer] += 1;
	}
	else
	{
		//cputsxy(0, 22, "SPACE");
	}
	return playerCounts[currentPlayer] == 4;
}

int calculateScore(unsigned char player)
{
	int score = 0;
	unsigned char value, counter;
	unsigned char counters[8];

	for(counter = 0; counter < 8; ++counter)
	{
		counters[counter] = 0;
	}

	for(counter = 0; counter < 4; ++counter)
	{
		if(playerTrays[player][counter] > 0)
		{
			value = playerTrays[player][counter] >> 4;
			counters[value] += 1;
			switch(value)
			{
			case COLOR_BLACK: score += 8 * counters[value]; break;
			case COLOR_RED: score += 4 * counters[value]; break;
			case COLOR_GREEN: score += 2 * counters[value]; break;
			case COLOR_BLUE: score += 1 * counters[value]; break;
			}
		}
	}	

	return score;
}